#include "CBalaPlanta.h"

//=============================================================================
CBalaPlanta::CBalaPlanta() {
    Type=ENTITY_TYPE_BALA;

    if (this->disparaIzq) {
        MoveLeft=true;
        MoveRight=false;
    } else {
        MoveRight=true;
        MoveLeft=false;
    }

}
//-----------------------------------------------------------------------------
void CBalaPlanta::SetDireccionDisparo(bool izquierda){
    this->disparaIzq=izquierda;
}//-----------------------------------------------------------------------------
void CBalaPlanta::OnLoop() {
		//We're not Moving
	if(MoveLeft == false && MoveRight == false &&
        MoveDown == false && MoveUp == false) {
		StopMove();
	}

	if(MoveLeft) {
		AccelX = -0.25;// -1.5;
		AccelY = 0;// -1.5;
	}else

	if(MoveRight) {
		AccelX = 0.25;// 1.5;
		AccelY = 0;// -1.5;
	}
    else
    if(MoveUp) {
		AccelY = -0.25;// -1.5;
		AccelX = 0;// -1.5;
	}else

	if(MoveDown) {
		AccelY = 0.25;// 1.5;
		AccelX = 0;// -1.5;
	}


	SpeedX += AccelX * CFPS::FPSControl.GetSpeedFactor();
	SpeedY += AccelY * CFPS::FPSControl.GetSpeedFactor();

	if(SpeedX > MaxSpeedX)  SpeedX =  MaxSpeedX;
	if(SpeedX < -MaxSpeedX) SpeedX = -MaxSpeedX;
	if(SpeedY > MaxSpeedY)  SpeedY =  MaxSpeedY;
	if(SpeedY < -MaxSpeedY) SpeedY = -MaxSpeedY;

	OnAnimate();
	OnMove(SpeedX, SpeedY);
}

//-----------------------------------------------------------------------------
void CBalaPlanta::OnRender(SDL_Surface* Surf_Display) {
	CEntity::OnRender(Surf_Display);
}

//------------------------------------------------------------------------------
void CBalaPlanta::OnCleanup() {
	CEntity::OnCleanup();
}
//------------------------------------------------------------------------------
void CBalaPlanta::OnAnimate() {
	if(SpeedX !=0 || SpeedY!=0) {
		Anim_Control.MaxFrames = 3;
	}else{
		Anim_Control.MaxFrames = 0;
	}
	CEntity::OnAnimate();
}

//------------------------------------------------------------------------------
bool CBalaPlanta::OnCollision(CEntity* Entity) {
    Inactive=true;
    return true;
}
//=============================================================================
void CBalaPlanta::OnMove(float MoveX, float MoveY) {
	if(MoveX == 0 && MoveY == 0) return;

	double NewX = 0;
	double NewY = 0;

	MoveX *= CFPS::FPSControl.GetSpeedFactor();
	MoveY *= CFPS::FPSControl.GetSpeedFactor();

	if(MoveX != 0) {
		if(MoveX >= 0) 	NewX =  CFPS::FPSControl.GetSpeedFactor();
		else 			NewX = -CFPS::FPSControl.GetSpeedFactor();
	}
    else
	if(MoveY != 0) {
		if(MoveY >= 0) 	NewY =  CFPS::FPSControl.GetSpeedFactor();
		else 			NewY = -CFPS::FPSControl.GetSpeedFactor();
	}

	while(true) {
		if(Flags & ENTITY_FLAG_GHOST) {
			PosValid((int)(X + NewX), (int)(Y + NewY)); //We don't care about collisions, but we need to send events to other entities

			X += NewX;
			Y += NewY;
		}else{
			if(PosValid((int)(X + NewX), (int)(Y))) {
				X += NewX;
			}else{ //colision con otro objeto!!
				/*SpeedX = (-1)*SpeedX;
				MoveLeft=!MoveLeft;
				MoveRight=!MoveRight;*/
				Inactive=true;
				SpeedX=0;
			}

			if(PosValid((int)(X), (int)(Y + NewY))) {
				Y += NewY;
			}else{
               /* if(MoveY > 0) {
                    //CanJump = true;
                }*/
				SpeedY = (-1)*SpeedY;
				MoveDown=!MoveDown;
				MoveUp=!MoveUp;
			}
		}

		MoveX += -NewX;
		MoveY += -NewY;

		if(NewX > 0 && MoveX <= 0) NewX = 0;
		if(NewX < 0 && MoveX >= 0) NewX = 0;

		if(NewY > 0 && MoveY <= 0) NewY = 0;
		if(NewY < 0 && MoveY >= 0) NewY = 0;

		if(MoveX == 0) NewX = 0;
		if(MoveY == 0) NewY = 0;

		if(MoveX == 0 && MoveY 	== 0) 	break;
		if(NewX  == 0 && NewY 	== 0) 	break;
	}
}
